package cn.sdhuijin.healthexam.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import cn.sdhuijin.healthexam.DatabaseHelper;
import cn.sdhuijin.healthexam.ExitApplication;
import cn.sdhuijin.healthexam.R;
import cn.sdhuijin.healthexam.control.AbstractControlActivity;
import cn.sdhuijin.healthexam.util.Constants;

/**
 * 
 * 
 * @author sunping
 */
public class SiSuiVisitSecondActivity extends AbstractControlActivity implements
		OnClickListener, OnCheckedChangeListener {

	// 创建日期ID
	private String createDate;
	private DatabaseHelper databaseHelper = new DatabaseHelper(this);
	private int[] checkHuanBingIds = { R.id.check_huanbingqingkuang_wu,
			R.id.check_huanbingqingkuang_fy, R.id.check_huanbingqingkuang_fx,
			R.id.check_huanbingqingkuang_ws };
	private int[] zhidao = { R.id.check_zd_kewy, R.id.check_zd_szfy,
			R.id.check_zd_yfjb, R.id.check_zd_yfywsh, R.id.check_zd_kqbj };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sisui_visit_second);
		ExitApplication.getInstance().addActivity(this);

		Intent intent = getIntent();
		Bundle bundle = intent.getBundleExtra("bundle");
		if (bundle != null) {
			createDate = bundle.getString("createDate");
		}
		setCurrentEditUserName(createDate);

		addCheckBoxGroupView(checkHuanBingIds, "两次随访间患病情况",
				R.id.edt_huanbingqingkuang_qt, "两次随访间其他患病情况", "无");

		CheckBox btFeiYan = (CheckBox) this
				.findViewById(R.id.check_huanbingqingkuang_fy);
		btFeiYan.setOnCheckedChangeListener(this);
		CheckBox btFuxie = (CheckBox) this
				.findViewById(R.id.check_huanbingqingkuang_fx);
		btFuxie.setOnCheckedChangeListener(this);
		CheckBox btWaiShang = (CheckBox) this
				.findViewById(R.id.check_huanbingqingkuang_ws);
		btWaiShang.setOnCheckedChangeListener(this);

		addEditTextView(R.id.edt_huanbingqingkuang_fy, "肺炎次数");
		addEditTextView(R.id.edt_huanbingqingkuang_fx, "腹泻次数");
		addEditTextView(R.id.edt_huanbingqingkuang_ws, "外伤次数");
		addCheckBoxView(zhidao, "指导");
		addEditTextView(R.id.edt_zd_qt, "指导其他");
		addRadioGroupView(R.id.rad_zhuanzhen, "转诊");
		addEditTextView(R.id.edt_zhuanzhen_yy, "转诊原因");
		addEditTextView(R.id.edt_zhuanzhen_jgjks, "机构及科室");
		addEditTextView(R.id.edt_next_date, "下次随访日期");
		addEditTextView(R.id.edt_visit_doctor, "随访医生签名");
		addEditTextView(R.id.edt_suggestion, "指导意见");

		// Properties文件路径
		controlManager.setPropertiesPath(Constants.PROPERTIES_PATH + createDate
				+ ".txt");
		Log.i(ChanQianVisitFirstActivity.class.getSimpleName(), "文件路径： "
				+ Constants.PROPERTIES_PATH + createDate + ".txt");
		controlManager.toControl();

		setEditTextListener(R.id.edt_next_date);
		setButtonListener(this, R.id.btn_complete, R.id.btn_previous);
	}

	/**
	 * 按钮点击处理事件
	 */
	@Override
	public void onClick(View arg0) {
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		// 获取点击按钮ID
		int id = (Integer) arg0.getId();
		switch (id) {
		case R.id.btn_complete:
			controlManager.toProperties();
			databaseHelper.updateIndexTable(createDate);
			this.toastInformation(R.string.toast_submit_info);
			intent.setClass(this, IndexActivity.class);
			startActivity(intent);
			break;
		case R.id.btn_previous:
			controlManager.toProperties();
			intent.setClass(this, SiSuiVisitFirstActivity.class);
			bundle.putString("createDate", createDate);
			intent.putExtra("bundle", bundle);
			startActivity(intent);
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (buttonView.getId() == R.id.check_huanbingqingkuang_fy) {
			EditText etFeiYan = (EditText) this
					.findViewById(R.id.edt_huanbingqingkuang_fy);
			if (isChecked == true) {
				etFeiYan.setEnabled(true);
			} else {
				etFeiYan.setText("");
				etFeiYan.setEnabled(false);
			}
		} else if (buttonView.getId() == R.id.check_huanbingqingkuang_fx) {
			EditText etFeiYan = (EditText) this
					.findViewById(R.id.edt_huanbingqingkuang_fx);
			if (isChecked == true) {
				etFeiYan.setEnabled(true);
			} else {
				etFeiYan.setText("");
				etFeiYan.setEnabled(false);
			}
		} else if (buttonView.getId() == R.id.check_huanbingqingkuang_ws) {
			EditText etFeiYan = (EditText) this
					.findViewById(R.id.edt_huanbingqingkuang_ws);
			if (isChecked == true) {
				etFeiYan.setEnabled(true);
			} else {
				etFeiYan.setText("");
				etFeiYan.setEnabled(false);
			}
		}

	}
}
